package com.lzh.excel.converter;

import com.alibaba.excel.converters.NullableObjectConverter;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.data.WriteCellData;

import java.math.BigDecimal;

/**
 * @author zhehen.lu
 * @date 2025/8/3 14:25
 */
public class EasyExcelNullableIntegerZeroConverter implements NullableObjectConverter<Integer> {
    public EasyExcelNullableIntegerZeroConverter() {
    }

    @Override
    public Class<?> supportJavaTypeKey() {
        return Integer.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.NUMBER;
    }

    @Override
    public WriteCellData<?> convertToExcelData(WriteConverterContext<Integer> context) throws Exception {
        Integer value = (Integer)context.getValue();
        return value == null ? new WriteCellData(new BigDecimal(0)) : new WriteCellData(new BigDecimal(value));
    }
}

